package com.imy.alscript;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.imy.virtual.client.core.VirtualCore;
import com.imy.virtual.helper.compat.NativeLibraryHelperCompat;
import com.imy.alscript.models.AppInfo;

import java.util.List;

import mirror.dalvik.system.VMRuntime;

public class PathListAdapter extends BaseSimpleListAdapter<AppInfo, PathListAdapter.Holder> {
  private Context mContext;
  private List mList;

  public PathListAdapter(Context context, List<AppInfo> infos) {
    super(context, infos);
  }

  @Override
  protected void bindView(int position, Holder holder, View convertView, AppInfo bean) {

    String packageName = bean.packageName;
    holder.packageTv.setText(packageName);
    holder.icon.setImageDrawable(bean.icon);
    boolean isInstall = bean.isInstall;
    if (isInstall){
      holder.nameTv.setText(bean.name);
    }else {
      holder.nameTv.setText(bean.name+"(未安装)");
    }
    int i = NativeLibraryHelperCompat.checkOnly64Support(bean.path, VMRuntime.is64Bit.call(VMRuntime.getRuntime.call()));
    if (i==NativeLibraryHelperCompat.ONLY_SUPPORT_64){
      holder.supportTv.setText("仅支持64位运行");
    }else if (i==NativeLibraryHelperCompat.SUPPORT_32){
      holder.supportTv.setText("仅支持32位运行");
    }else if (i==NativeLibraryHelperCompat.SUPPORT_64_AND_32){
      holder.supportTv.setText("支持32位与64运行");
    }

    holder.startText.setOnClickListener(v -> {
      if (mStartClickedListener!=null){
        mStartClickedListener.onStart(bean);
      }
    });
    holder.installText.setOnClickListener(v -> {
      if (mStartClickedListener!=null){
        mStartClickedListener.onInstall(bean);
      }
    });
    holder.checkSoText.setOnClickListener(v -> {
      if (mStartClickedListener!=null){
        mStartClickedListener.onCheckSo(bean);
      }
    });
  }

  public interface StartClickedListener{
    void onStart(AppInfo path);
    void onInstall(AppInfo path);
    void onCheckSo(AppInfo path);
  }
  public void setStartClickedListener(StartClickedListener listener){
    this.mStartClickedListener =listener;
  }
  private StartClickedListener mStartClickedListener;
  @Override
  protected int getLayoutId() {
    return R.layout.path_item;
  }

  @Override
  public Holder getHolder(View view) {
    return new Holder(view);
  }

  public class Holder extends BaseViewHolder {

    TextView nameTv;
    ImageView icon;
    TextView packageTv;
    TextView startText;
    TextView installText;
    TextView checkSoText;
    TextView supportTv;
    public Holder(View itemView) {
      super(itemView);
      nameTv =itemView.findViewById(R.id.apk_name);
      icon =itemView.findViewById(R.id.icon_iv);
      packageTv =itemView.findViewById(R.id.package_tv);
      startText= itemView.findViewById(R.id.start_text);
      installText =itemView.findViewById(R.id.install_text);
      checkSoText =itemView.findViewById(R.id.check_so_text);
      supportTv= itemView.findViewById(R.id.bit_support_tv);
    }
  }
}
